####################################################
#Author: Kelli Marquardt
#Purpose: Install necessary packages (and dictionaries) for replication package 
#FOLLOW STEPS IN ORDER! 
####################################################

rm(list = ls(all.names = TRUE))


############################
#1 Download WordNet dictionary 
############################
#step 1- visit link: https://wordnet.princeton.edu/download/current-version
#step 2- download WordNet 2.1 for Windows and take note of the location (will be needed in Step2a_PrepNLPtokens.R)
install.packages("wordnet") #step 3- install wordnet package 



############################
#2 Download GLOVE word embeddings
############################
#step 1- visit https://nlp.stanford.edu/projects/glove/ 
#step 2- download "glove.6B.zip" under "Download pre-trained word vectors" 
#step 3- unzip the download to a subfolder named "Glove" in Data folder 
#step 4- run the next few lines to confirm download location 
glove_file = file.path("..", "data", "Glove", "glove.6B.300d.txt")
if (!file.exists(glove_file)) {
  stop("Make sure GloVe embeddings are saved in data/Glove. Follow setup instructions above.")
}
rm(glove_file)

############################
#3 Install and create r Java environment 
############################
install.packages("rJavaEnv") #step 1- install package
library(rJavaEnv) #step 2- load library 
rje_consent(provided = TRUE)
java_quick_install(version = 21) #step 3- install environment. When prompted, type "yes" 

#step 4- run the next few lines to confirm rjavaenv is now a subfolder in Code folder
rjava_path = file.path("rjavaenv")
if (!dir.exists(rjava_path)) {
  stop("Make sure rjavaenv is installed and set in Code/rjavaenv. Follow setup instructions above.")
}
rm(rjava_path)

############################
#4 Install other required packages 
############################

required.packages = c("dplyr", "ggplot2", "textclean",
                      "data.table", "tidytext", "SnowballC",
                      "tidyr", "mlr", "randomForest",
                      "parallel", "parallelMap", "pbapply",
                      "stringr", "hunspell", "ggwordcloud",
                      "textstem", "tm", "MASS", 
                      "fastDummies", "sandwich", "lmtest", 
                      "cowplot", "scales", "ipumsr", 
                      "openNLP", "NLP")

missing=setdiff(required.packages, rownames(installed.packages()))

if (length(missing) > 0) {
  message("Installing missing packages: ", paste(missing, collapse = ", "))
  install.packages(missing, dependencies = TRUE)
} else {
  message("All required packages are already installed.")
}
rm(missing, required.packages)


############################
#5 Download and set Medical Dictionary 
############################
#step 1- download and extract from https://github.com/glutanimate/hunspell-en-med-glut-workaround.git
#step 2- save to a subfolder in Data folder : data/hunspell-en-med-glut-workaround-master
#step 3- load hunspell package (run next line)
library(hunspell)
#step 4- run following lines to confirm medical dictionary was downloaded and saved successfully 
Sys.setenv(DICPATH = file.path("..", "data", "hunspell-en-med-glut-workaround-master"))
dictionary("en_US")
med_dict_check=hunspell_suggest("Olanzapinee")
if (med_dict_check[[1]][1]!="Olanzapine") {
  stop("Make sure medical dictionary is saved in data/hunspell-en-med-glut-workaround-master. Follow setup instructions above.")
}
rm(med_dict_check)


